﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DemoFacebookAPI
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            string OauthURL =
                @"https://www.facebook.com/dialog/oauth?client_id=" + AppSettings.Default.AppID
                + "&redirect_uri=https://www.facebook.com/connect/login_success.html&response_type=token&scope=" + AppSettings.Default.Scope;
            WebBrowserLogin.Navigate(OauthURL);
        }

        private void WebFacebook_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            string access_token;
            if (WebBrowserLogin.Url.AbsoluteUri.Contains("access_token"))
            {
                string url1 = WebBrowserLogin.Url.AbsoluteUri;
                string url2 = url1.Substring(url1.IndexOf("access_token") + 13);
                access_token = url2.Substring(0, url2.IndexOf("&"));
                AppSettings.Default.AccessToken = access_token;

                //frmMain.IsLogin = true;
                //frmMain.IsBackFromLogin = true;

                frmMain frm = new frmMain(this);
                this.Hide();
                frm.Show();
                

                //FacebookClient fb = new FacebookClient(access_token);

                //dynamic data = fb.Get("/me");


                //MessageBox.Show("id=" + data.id + Environment.NewLine + "name=" + data.name + Environment.NewLine + "Link=" + data.link);
            }
        }
    }
}
